"""
File: testcase.py
Author: ershixiong
Date: 2024/3/22
describe:

"""
from typing import Any

from pydantic import BaseModel

from ddt_framework_02.service import service, Service
from ddt_framework_02.utils import logger
from ddt_framework_02.web import web, Web


class TestCase(BaseModel):
    name: str = None
    type: str = None
    given: list[dict] = []
    when: list[dict] = []
    then: list[dict] = []

    def __init__(self, **data: Any) -> None:
        super().__init__(**data)
        self.type = data.get('type')

    def run(self):
        logger.debug('start run')

        # 依次遍历given、when、then，执行每一步

        for step in self.given:
            self.run_step(step)

        for step in self.when:
            self.run_step(step)

        for step in self.then:
            self.run_step(step)

    def run_step(self, step):
        global service, web

        if self.type == 'api_test':
            if service is None:
                service = Service()
            service.run_step(step)
        elif self.type == 'ui_test':
            if web is None:
                web = Web()
            web.run_step(step)
